package cn.xspace.wnx.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.List;

import cn.xspace.wnx.R;
import cn.xspace.wnx.entity.GroupEntity;
import cn.xspace.library.bean.Type;

/**
 * Created by liuchen on 2016/11/1.
 */

public class GroupAdapter extends BaseAdapter {

    private Context mContext;
    private List<GroupEntity> dataList;

    public GroupAdapter(Context context, List<GroupEntity> dataList) {
        mContext = context;
        this.dataList = dataList;
    }

    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public GroupEntity getItem(int position) {
        return dataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return dataList.get(position).hashCode();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        GroupEntity groupEntity = getItem(position);
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.listitem_group, null);
            viewHolder = new ViewHolder();
            viewHolder.findView(convertView);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.setData(groupEntity);


        return convertView;
    }

    class ViewHolder {
        ImageView headImg;
        TextView nameTxt;

        public void findView(View view) {
            this.headImg = (ImageView) view.findViewById(R.id.img_head);
            this.nameTxt = (TextView) view.findViewById(R.id.txt_name);
            view.setTag(this);
        }

        public void setData(GroupEntity entity) {

            if(entity.group_type == Type.GroupType.ORGANIZATION){
                this.headImg.setImageResource(R.drawable.icon_default_org);
            }else if(entity.group_type == Type.GroupType.PROFESSIONAL){
                this.headImg.setImageResource(R.drawable.icon_default_profess);
            }else {
                if (TextUtils.isEmpty(entity.group_logo)) {
                    this.headImg.setImageResource(R.drawable.icon_header_default);
                } else {
                    Picasso.with(mContext)
                            .load(entity.group_logo)
                            .placeholder(R.drawable.icon_header_default)
                            .error(R.drawable.icon_header_default)
                            .into(this.headImg);
                }
            }


            this.nameTxt.setText(entity.group_name);
        }
    }
}
